<?php
class ControlGroup {

  private $xml_text;
  private $xml_subtext_template;
  private $xml_subtext;
  private $xml_optsubtext;
  private $xml_optsubtext_template;
  private $control_counter;
  private $javaScript;

  public function __construct($id, $text, $xml_text, $xml_subtext, $xml_optsubtext) {
    $this->xml_text = str_replace("%GROUP_TL%", $text, str_replace("%GROUP_ID%", $id, $xml_text));
    $this->xml_subtext_template = $xml_subtext;
    $this->xml_optsubtext_template = $xml_optsubtext;
    $this->control_counter = 0;
    $this->javaScript = array();
  }

  public function addControl($type, $id, $text, $value) {
    $tmp = str_replace("%TYPE%", $type, $this->xml_subtext_template[1]);
    $tmp = str_replace("%NODE_ID%", $id, $tmp);
    $tmp = str_replace("%NODE_TL%", $text, $tmp);
    $tmp = str_replace("%VALUE%", $value, $tmp);
    $this->xml_subtext[$this->control_counter] = $tmp;
    $this->control_counter++;
    return $this->control_counter - 1;
  }

  public function addOption($id, $text, $selected, $control_id) {
    $opt = str_replace("%OPT_ID%", $classes[$i]["id"], $this->xml_optsubtext_template[1]);
    $opt = str_replace("%OPT_TL%", $classes[$i]["text"], $opt);
    if ($classes[$i]["id"] == $default_class)
      $opt = str_replace("%SELECTED%", "selected", $opt);
    else
      $opt = str_replace("%SELECTED%", "0", $opt);
    $tmp = str_replace("%OPT_ID%", $id, $this->xml_optsubtext_template[1]);
    $tmp = str_replace("%OPT_TL%", $text, $tmp);
    $tmp = str_replace("%SELECTED%", $selected, $tmp);
    $this->xml_subtext[$control_id] = str_replace($this->xml_optsubtext_template[0], $tmp.$this->xml_optsubtext_template[0], $this->xml_subtext[$control_id]);
  }

  public function getResult() {
  	
    $tmp = "";
    for ($i = 0; $i < $this->control_counter; $i++) {
      //$tmp .= str_replace($this->xml_optsubtext_template[0], "", $this->xml_subtext[$i]);
      $tmp .= $this->getControl($i);
    }
    $jS = "";
    for ($i = 0, $size = sizeof($this->javaScript); $i < $size; $i++) {
    	$jS .= "<script exec=\"".$this->javaScript[$i]."\" />";
    }
    return str_replace($this->xml_subtext_template[0], $tmp.$jS, $this->xml_text);
  }
  
  public function getControl ($id) {
  	return str_replace($this->xml_optsubtext_template[0], "", $this->xml_subtext[$id]);
  }
  
  public function addJavaScript ($script) {
  	$this->javaScript[] = $script;
  }

    //$btns = str_replace($datmatches[0], /*$btn3.*/$btn1.$btn2.$btn3, $matches[1]);
    //$btns = str_replace("%GROUP_ID%", "1", $btns);
    //$btns = str_replace("%GROUP_TL%", getMessage("Action"), $btns);

}

?>